package top.infopub.mgr.qrtz.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.dubbo.vo.job.QuartzJobVo;
import top.infopub.dubbo.ws.job.QuartzJobService;
import top.infopub.generic.GenericController;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.qrtz.service.JobConsoleService;
import top.infopub.mgr.qrtz.vo.JobTriggersVo;
import top.infopub.mgr.util.LogUtil;
import top.infopub.model.qrtz.JobTriggers;
import top.infopub.vo.ResponseVo;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;


/**
 * 调度任务管理
 * @author Awoke
 * @version 2018年10月15日
 * @see JobConsoleController
 * @since
 */
@Controller
@RequestMapping("/jobConsole")
public class JobConsoleController extends GenericController {

    @Autowired
    private JobConsoleService jobConsoleService;

    // dubbo服务
    @Autowired
    private QuartzJobService jobService;

    /**
     * 日志列表
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "qrtz/layjob-console";
    }

    /**
     * 根据条件获取获取列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    public @ResponseBody RspVo<JobTriggers> queryDataByCondition(JobTriggersVo vo,
                                                                 ParamPage paramPage) {
        Page<JobTriggers> page = new Page<JobTriggers>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = jobConsoleService.selectDataByCondition(page, vo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 新增job
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public RspVo<String> addJob(QuartzJobVo vo) {
        ResponseVo<String> response = jobService.addJob(vo);
        if (response.isSuccess()) {
            // 操作日志
            LogUtil.bizLog(LogUtil.LogModelType.QRTZJOB, LogUtil.AcctionType.ADD,
                "新增job " + JSON.toJSONString(vo), "");
        }
        return new RspVo<String>(response.isSuccess(), response.getMsg());
    }

    /**
     * 修改job
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public RspVo<String> updateJob(QuartzJobVo vo) {
        ResponseVo<String> response = jobService.updateJob(vo);
        if (response.isSuccess()) {
            // 操作日志
            LogUtil.bizLog(LogUtil.LogModelType.QRTZJOB, LogUtil.AcctionType.UPDATE,
                "修改job " + JSON.toJSONString(vo), "");
        }
        return new RspVo<String>(response.isSuccess(), response.getMsg());
    }

    /**
     * 删除job
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public RspVo<String> deleteJob(QuartzJobVo vo) {
        ResponseVo<String> response = jobService.deleteJob(vo);
        if (response.isSuccess()) {
            // 操作日志
            LogUtil.bizLog(LogUtil.LogModelType.QRTZJOB, LogUtil.AcctionType.DEL,
                "删除job jobName:" + vo.getJobName() + " triggerName:" + vo.getTriggerName(), "");
        }
        return new RspVo<String>(response.isSuccess(), response.getMsg());
    }

    /**
     * 暂停job
     * @return
     */
    @RequestMapping("/pause")
    @ResponseBody
    public RspVo<String> pauseJob(QuartzJobVo vo) {
        ResponseVo<String> response = jobService.pauseJob(vo);
        if (response.isSuccess()) {
            // 操作日志
            LogUtil.bizLog(LogUtil.LogModelType.QRTZJOB, LogUtil.AcctionType.UPDATE,
                "暂停job jobName:" + vo.getJobName() + " triggerName:" + vo.getTriggerName(), "");
        }
        return new RspVo<String>(response.isSuccess(), response.getMsg());
    }

    /**
     * 启用job
     * @return
     */
    @RequestMapping("/resume")
    @ResponseBody
    public RspVo<String> resumeJob(QuartzJobVo vo) {
        ResponseVo<String> response = jobService.resumeJob(vo);
        if (response.isSuccess()) {
            // 操作日志
            LogUtil.bizLog(LogUtil.LogModelType.QRTZJOB, LogUtil.AcctionType.UPDATE,
                "启用job jobName:" + vo.getJobName() + " triggerName:" + vo.getTriggerName(), "");
        }
        return new RspVo<String>(response.isSuccess(), response.getMsg());
    }

}
